package com.aisile.user.service.impl;

import com.aisile.common.exception.CustomExceptionCatch;
import com.aisile.model.article.pojos.ApAuthor;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.model.user.dtos.UserRelationDto;
import com.aisile.model.user.pojos.ApUser;
import com.aisile.model.user.pojos.ApUserFan;
import com.aisile.model.user.pojos.ApUserFollow;
import com.aisile.user.client.UserFeign;
import com.aisile.user.mapper.ApUserFanMapper;
import com.aisile.user.mapper.ApUserFollowMapper;
import com.aisile.user.mapper.ApUserMapper;
import com.aisile.user.service.UserRelationService;
import com.aisile.utils.threadlocal.AppThreadLocalUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;

/**
 * @Auther:yry
 * @Date:2024/1/13 0013
 * @VERSON:1.0
 */
@Service
@SuppressWarnings("ALL")
public class UserRelationServiceImpl implements UserRelationService {

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private UserFeign userFeign;

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        if (dto == null) {
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 参数校验
        if (dto.getAuthorId() == null || dto.getOperation() == null) {
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 0 1
        if (dto.getOperation() < 0 || dto.getOperation() > 1) {
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 获取当前用户信息（用户id）
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null || apUser.getId() == 0L) {
            // 没有登录权限
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 当前登录的用户信息
        apUser = apUserMapper.selectById(apUser.getId());
        // 获取作者信息
        ApAuthor apAuthor = userFeign.findById(dto.getAuthorId());
        if (apAuthor == null) {
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 关注
        if (dto.getOperation() == 0) {
            return this.followByUserId(apAuthor, apUser);
        } else {
            // 取消关注
            return this.followCancelByUserId(apAuthor,apUser);
        }
    }


    public ResponseResult followCancelByUserId(ApAuthor apAuthor, ApUser apUser) {
        //  关注表 粉丝表
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(
                Wrappers.<ApUserFollow>lambdaQuery().eq(
                        ApUserFollow::getFollowId, apAuthor.getUserId()
                ).eq(
                        ApUserFollow::getUserId, apUser.getId()
                )
        );
        if (apUserFollow != null){
            apUserFollowMapper.deleteById(apUserFollow.getId());
        }
        // 粉丝表
        ApUserFan apUserFan = apUserFanMapper.selectOne(
                Wrappers.<ApUserFan>lambdaQuery().eq(
                        ApUserFan::getFansId, apUser.getId()
                ).eq(
                        ApUserFan::getUserId, apAuthor.getUserId()
                )
        );
        if (apUserFan != null){
            apUserFanMapper.deleteById(apUserFan.getId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 关注作者
     *
     * @param apAuthor 作者信息
     * @param apUser   用户信息
     * @return
     */
    public ResponseResult followByUserId(ApAuthor apAuthor, ApUser apUser) {
        //  关注表 粉丝表
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(
                Wrappers.<ApUserFollow>lambdaQuery().eq(
                        ApUserFollow::getFollowId, apAuthor.getUserId()
                ).eq(
                        ApUserFollow::getUserId, apUser.getId()
                )
        );
        if (apUserFollow == null) {
            apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(apUser.getId());
            apUserFollow.setFollowId(apAuthor.getUserId());
            apUserFollow.setFollowName(apAuthor.getName());
            apUserFollow.setLevel((short) 0);
            apUserFollow.setIsNotice(true);
            apUserFollow.setCreatedTime(new Date());
            apUserFollowMapper.insert(apUserFollow);
        }
        // 粉丝表
        ApUserFan apUserFan = apUserFanMapper.selectOne(
                Wrappers.<ApUserFan>lambdaQuery().eq(
                        ApUserFan::getFansId, apUser.getId()
                ).eq(
                        ApUserFan::getUserId, apAuthor.getUserId()
                )
        );
        if (apUserFan == null) {
            apUserFan = new ApUserFan();
            apUserFan.setUserId(apAuthor.getUserId());
            apUserFan.setFansId(apUser.getId().longValue());
            apUserFan.setFansName(apUser.getName());
            apUserFan.setLevel((short) 0);
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay(true);
            apUserFan.setIsShieldLetter(false);
            apUserFan.setIsShieldComment(false);
            apUserFanMapper.insert(apUserFan);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
